package com.itheima.reggie.utils;

import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 发送短信验证码工具类
 * @author Xiao
 * @date 2021/9/29
 */
@Slf4j
public class SMSUtils {
    /**
     * 发送短信
     * @param phoneNumbers  手机号
     * @param param 参数
     */
    public static void sendMessage(String phoneNumbers,String param) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                "LTAI5tE8vPcwHVnaQLtxJzjc",
                "sCu3on9RmHxv7tqGIRS32OvRNu2ZtV");//自己账号的AccessKey信息
        IAcsClient client = new DefaultAcsClient(profile);
        /*CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");//短信服务的服务接入地址
        request.setSysVersion("2017-05-25");//API的版本号
        request.setSysAction("SendSms");//API的名称
        request.putQueryParameter("PhoneNumbers", "1503871****");//接收短信的手机号码
        request.putQueryParameter("SignName", "阿里大于测试专用");//短信签名名称
        request.putQueryParameter("TemplateCode", "SMS_209335004");//短信模板ID
        request.putQueryParameter("TemplateParam", "{\"code\":\"1111\"}");//短信模板变量对应的实际值*/
        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phoneNumbers);  //接收短信的手机号码
        request.setSignName("青橙");  //短信签名名称
        request.setTemplateCode("SMS_205136201");   //短信模板ID
        request.setTemplateParam("{\"code\":\""+param+"\"}");   //短信模板变量对应的实际值
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            //System.out.println(response.getData());
            log.info("短信发送成功！--> {}", response.getMessage());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
